package com.example.shiyansan.service.impl;

import com.example.shiyansan.entity.Order;
import com.example.shiyansan.mapper.OrderMapper;
import com.example.shiyansan.service.ExportService;
import com.example.shiyansan.vo.OrderVo;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import static com.example.shiyansan.interceptor.JwtTokenInterceptor.THREAD_LOCAL;

@Service
public class ExportServiceImpl implements ExportService {

    @Autowired
    private OrderMapper orderMapper;
    /**
     * 导出订单信息报表
     * @param response
     */
    @Override
    public void exportBusinessData(HttpServletResponse response) throws IOException {
        // 先设置响应头，再获取输出流
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-Disposition", "attachment; filename=orders.xlsx");

        Long userId = (Long) THREAD_LOCAL.get();
        List<OrderVo> orderVoList = orderMapper.listVo(userId);

        try (InputStream in = this.getClass().getClassLoader().getResourceAsStream("templates/订单导出.xlsx");
             XSSFWorkbook excel = new XSSFWorkbook(in);
             ServletOutputStream out = response.getOutputStream()) {

            XSSFSheet sheet1 = excel.getSheet("Sheet1");

            int i = 0;
            for (OrderVo orderVo : orderVoList) {
                XSSFRow row = sheet1.getRow(3 + i++);
                if (row == null) {
                    row = sheet1.createRow(3 + i - 1);
                }

                // 确保单元格存在
                for (int j = 1; j <= 5; j++) {
                    if (row.getCell(j) == null) {
                        row.createCell(j);
                    }
                }

                // 转换订单状态
                String status = "";
                switch(orderVo.getStatus()) {
                    case "0":
                        status = "待支付";
                        break;
                    case "1":
                        status = "已支付";
                        break;
                    case "2":
                        status = "已发货";
                        break;
                    case "3":
                        status = "已完成";
                        break;
                    default:
                        status = "未知状态";
                }

                row.getCell(1).setCellValue(orderVo.getUsername());
                row.getCell(2).setCellValue("¥" + orderVo.getTotalPrice().toString());
                row.getCell(3).setCellValue(status);
                row.getCell(4).setCellValue(orderVo.getCreateTime().toString());
                row.getCell(5).setCellValue(orderVo.getCombinedDetails());

                // 设置自动换行
                CellStyle wrapStyle = excel.createCellStyle();
                wrapStyle.setWrapText(true);
                row.getCell(4).setCellStyle(wrapStyle);
                row.getCell(5).setCellStyle(wrapStyle);
            }

            // 调整列宽
            for (int j = 1; j <= 5; j++) {
                sheet1.autoSizeColumn(j);
            }
            excel.write(out);
        }
    }
}
